<template>
  <div class="number-box">
    <p class="box-item" v-for="(item,index) in computeNumber" :key="index">
      <span ref="numberDom">0<br />1<br />2<br />3<br />4<br />5<br />6<br />7<br /> 8<br /> 9<br /></span>
    </p>
  </div>
</template>

<script>
export default {
  name: "No1Test",
  props: {
    number: {
      type: Number,
      default: 0,
    },
  },
  data() {
    return {
      maxLen: 8, //数字最大长度
      computeNumber: "",
      timeTicket: null,
    };
  },
  mounted() {
    this.increaseNumber();
  },
  destroyed() {
    window.clearTimeout(this.timeTicket);
    this.timeTicket = null;
  },
  methods: {
    getRandomNumber(min, max) {
      return Math.floor(Math.random() * (max - min + 1) + min);
    },
    //    设置每一个数字的位偏移
    setNumberTransform() {
      let numberItems = this.$refs.numberDom;
      for (let index = 0; index < numberItems.length; index++) {
        let elem = numberItems[index];
        elem.style.transform = `translate(-50%,-${Number(
          this.computeNumber[index]
        ) * 10}%)`;
      }
    },
    increaseNumber() {
      this.refresh();
      this.timeTicket = setTimeout(this.increaseNumber, 5000);
    },
    // i定时刷新数据
    refresh() {
      this.computeNumber = this.prefixZero(this.number, this.manLen);
      this.$nextTick(() => this.setNumberTransform());
    },
  },
  watch: {
    number() {
      this.refresh();
    },
  },
};
</script>
<style scoped>
.number-box {
  width: 56.8%;
  display: flex;
  justify-content: center;
  margin: 26px auto 0;
}
.number-box p {
  flex: 1;
  height: 163px;
  line-height: 163px;
  background: rgb(139, 112, 112);
  margin: 0 26px 0 0;
  color: #ffffff;
  text-align: center;
  border-radius: 12px;
  padding: 0 19px;
  display: inline-block;
  font-size: 62px;
  position: relative;
  writing-mode: vertical-lr;
  text-orientation: upright;
  overflow: hidden;
}
.number-box p:last-child {
  margin-right: 0px;
}
.number-box span {
  color: #9185e0;
  font-size: 118px;
  font-weight: bolder;
  position: absolute;
  top: 10px;
  left: 50%;
  transform: translate(-50%, -50%);
  transition: transform 2s;
  letter-spacing: 10px;
}
</style>
